package com.example.jsonarry.nanningtravel.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import com.example.jsonarry.nanningtravel.R;
import com.example.jsonarry.nanningtravel.custom.RoundedImageView;

public class MyStaggedRecyclerAdapter extends RecyclerView.Adapter<MyStaggedRecyclerAdapter.MyViewHolder>{

	private List<String> list;
	private Context context;
	private OnItemClickListener mOnItemClickListener;
	private OnItemLongClickListener mOnItemLongClickListener;
	private ArrayList<Integer> heights;

	public MyStaggedRecyclerAdapter(Context context, List<String> list) {
		this.list = list;
		this.context = context;
		heights = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			heights.add((int) (200+Math.random()*50));
		}
	}
	
	class MyViewHolder extends ViewHolder{
		RoundedImageView roundedImageView;

		public MyViewHolder(View view) {
			super(view);
			roundedImageView = (RoundedImageView) view.findViewById(R.id.num);
		}
		
	}

	@Override
	public int getItemCount() {
		return list.size();
	}

	@Override
	public void onBindViewHolder(final MyViewHolder holder, final int position) {
		// 绑定数据给view
		LayoutParams params = holder.roundedImageView.getLayoutParams();
		params.height = heights.get(position);
		holder.roundedImageView.setBackgroundColor(Color.rgb(100, heights.get(position)-45, heights.get(position)-45));
		holder.roundedImageView.setLayoutParams(params);
//		holder.roundedImageView.setText(list.get(position));
		//绑定点击事件
		if(mOnItemClickListener!=null){
			holder.itemView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					int p = holder.getLayoutPosition();
					mOnItemClickListener.onItemClick(v, p);
				}
			});
		}
		if(mOnItemLongClickListener!=null){
			holder.itemView.setOnLongClickListener(new OnLongClickListener() {
				
				@Override
				public boolean onLongClick(View v) {
					int p = holder.getLayoutPosition();
					mOnItemLongClickListener.onItemLongClick(v, p);
					return true;
				}
			});
		}
	}

	@Override
	public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
		// 创建视图
		MyViewHolder holder = new MyViewHolder(View.inflate(context, R.layout.item, null));
		return holder;
	}
	
	public void deleteData(int position){
		list.remove(position);
		//刷新适配器
//		notifyDataSetChanged();
		//刷新局部---性能上有很大的提高。
		notifyItemRemoved(position);
	}
	
	public void addData(int position){
		list.add(position,position+"  data");
		heights.add(position, (int) (200+Math.random()*50));
		//刷新适配器
//		notifyDataSetChanged();
		//刷新局部---性能上有很大的提高。
		notifyItemInserted(position);
	}
	
	public interface OnItemClickListener{
		void onItemClick(View view, int position);
	}
	public interface OnItemLongClickListener{
		void onItemLongClick(View view, int position);
	}
	
	public void setOnItemClickListener(OnItemClickListener listener){
		this.mOnItemClickListener = listener;
	}
	public void setOnItemClickLongListener(OnItemLongClickListener listener){
		this.mOnItemLongClickListener = listener;
	}
	
}
